package com.ubisolv.vmakeup.util;

import java.io.IOException;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.ubisolv.vmakeup.app.Const;

public class ImageUtil {

	private ImageUtil() {}
	
	public static int getExifRotation(final String imagePath) {
		int rotation =	0;
		
		try {
			ExifInterface exif =	new ExifInterface(imagePath);
			int exifRotation =		exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
			rotation =				0;
			
			switch (exifRotation) {
			case ExifInterface.ORIENTATION_ROTATE_90: 	rotation = 90; 	break;
			case ExifInterface.ORIENTATION_ROTATE_180: 	rotation = 180; break;
			case ExifInterface.ORIENTATION_ROTATE_270: 	rotation = 270; break;
			}
			
			Log.d("getExifRotation", String.format("image exifRotation:[%d], rotation:[%d]", exifRotation, rotation));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return rotation;
	}
	
	public static String getRealPath(ContentResolver resolver, final Uri uri) {
		Cursor cursor =	resolver.query(uri, null, null, null, null);
		
		String result =	"";
		
		if (cursor == null) {
			Log.d("getRealPath", "cursor is null");
			result =	uri.getPath();
		} else {
			cursor.moveToFirst();
			
			int idx = 	cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
			result =	cursor.getString(idx);
			
			Log.d("getRealPath", String.format("real path:[%s]", result));
		}
		
		return result;
	}
	
		private static SZ getImageSize(ContentResolver resolver, Uri uri) {
			final String realPath =			getRealPath(resolver, uri);
			final Options options =			new BitmapFactory.Options();
			
			options.inJustDecodeBounds =	true;
			BitmapFactory.decodeFile(realPath, options);
			
			return new SZ(options.outWidth, options.outHeight);
		}

		private static SZ getImageSize(final String path) {
			final Options options =			new BitmapFactory.Options();
			
			options.inJustDecodeBounds =	true;
			BitmapFactory.decodeFile(path, options);
			
			return new SZ(options.outWidth, options.outHeight);
		}
		
	public static Bitmap getBitmapByUri(ContentResolver resolver, Uri uri) {
		final String path =				getRealPath(resolver, uri);
		final SZ size =					getImageSize(path);
		
		final int length =				Math.max(size.width, size.height);
		final int scale =				Math.max(
				(int)Math.floor((float)length / (float)Const.DEFAULT_IMAGE_HEIGHT), 1
		);		
		
		final Options options =			new BitmapFactory.Options();
		options.inSampleSize =			scale;
		
		final Bitmap bitmap =			BitmapFactory.decodeFile(path, options);
		
		int rotation =					getExifRotation(path);
		
		Bitmap newBitmap =				null;;
		if (rotation != 0) {
			Matrix matrix =	new Matrix();
			matrix.postRotate(rotation);
			newBitmap =	Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		} else {
			newBitmap =	Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
		}

		Log.d("getBitmapByUri", 
				String.format("size:[%dx%d], length:[%d], scale:[%d], rotation:[%d], new size:[%dx%d]",
						size.width, size.height, length, scale, rotation, newBitmap.getWidth(), newBitmap.getHeight()
				)
		);
		
		return newBitmap;
	}
}
